package org.example.cicdi.service.fieldchain;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import org.example.cicdi.enums.EnumWhiteColumnAnnotation;
import org.example.cicdi.service.impl.org.FieldAnnotationService;
import org.example.codegenerate.modelext.WebVo.domian.column.ColumnVoDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class FieldChainFacaService {
    @Autowired
    private List<FieldAnnotationService> fieldServices;
    public List<ColumnVoDomain> handleFieldAnnotations(Class<?> cl) throws Exception{
        Field[]fields = cl.getDeclaredFields();
        if (fields == null || fields.length == 0) {
            throw new Exception("对象可用字段为空");
        }

        List<ColumnVoDomain>list = new ArrayList<>();
        for (Field field : fields) {
            field.setAccessible(true);
            ColumnVoDomain columnVoDomain = new ColumnVoDomain();
            if (CollectionUtils.isEmpty(fieldServices)) {
                break;
            }

            for (FieldAnnotationService service : fieldServices) {
                if (EnumWhiteColumnAnnotation.getByCode(service.getCode()) != null) {
                    service.handleFieldVo(field, columnVoDomain);
                }
            }

            if (columnVoDomain.getDbVo() != null && StringUtils.isNotBlank(columnVoDomain.getDbVo().getDbFieldName())) {
                list.add(columnVoDomain);
            }
        }
        return list;
    }

}
